package com.android.flowoflife.adapters;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.android.flowoflife.R;

public class TestAdapter extends BaseAdapter {
	Context mContext;
	public TestAdapter(Context context) {
		this.mContext = context;
	}
	@Override
	public int getCount() {
		return 2;
	}

	@Override
	public Object getItem(int position) {
		Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.RGB_565);
		Canvas canvas = new Canvas(bitmap);
		if (position == 1) {
			Drawable image1 = mContext.getResources().getDrawable(R.drawable.image_1);
			image1.setBounds(new Rect(0, 0, 50, 50));
			image1.draw(canvas);
			Drawable image2 = mContext.getResources().getDrawable(R.drawable.image_2);
			image2.setBounds(new Rect(50, 0, 100, 50));
			image2.draw(canvas);
			Drawable image3 = mContext.getResources().getDrawable(R.drawable.image_3);
			image3.setBounds(new Rect(0, 50, 50, 100));
			image3.draw(canvas);
			Drawable image4 = mContext.getResources().getDrawable(R.drawable.image_4);
			image4.setBounds(new Rect(50, 50, 100, 100));
			image4.draw(canvas);
		} else {
			Drawable image1 = mContext.getResources().getDrawable(R.drawable.image_5);
			image1.setBounds(new Rect(0, 0, 50, 50));
			image1.draw(canvas);
			Drawable image2 = mContext.getResources().getDrawable(R.drawable.image_6);
			image2.setBounds(new Rect(50, 0, 100, 50));
			image2.draw(canvas);
			Drawable image3 = mContext.getResources().getDrawable(R.drawable.image_7);
			image3.setBounds(new Rect(0, 50, 50, 100));
			image3.draw(canvas);
			Drawable image4 = mContext.getResources().getDrawable(R.drawable.image_8);
			image4.setBounds(new Rect(50, 50, 100, 100));
			image4.draw(canvas);
		}
		return bitmap;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ImageView i;

        if (convertView == null) {
            i = new ImageView(mContext);
            i.setScaleType(ImageView.ScaleType.FIT_CENTER);
            i.setLayoutParams(new GridView.LayoutParams(100, 100));
        } else {
            i = (ImageView) convertView;
        }

        i.setImageBitmap((Bitmap) getItem(position));

        return i;
	}

}
